#include "FIModule.h"
#include "FIDefines.h"
#include "CmdDefines.h"
#include "FIOverviewsDlg.h"
#include <bondlib/BondContainer.h>
#include <guiframe/IModule.h>
#include <msgcenter/FIMsgProcess.h>
#include <msgcenter/IRSMsgProcess.h>
#include <msgcenter/ExchRepoProcess.h>
#include <qbtools/message/RequestNotifier.h>
#include <qbkernel/include/qb_base.h>
#include <future>
#include <tfbondlib/tfBondDataCenter.h>

using namespace qb;

extern "C" S_QBFIXEDINCOME_EXPORT IModule * getModule()
{
	return &FIModule::instance();
}

FIModule& FIModule::instance()
{
	static FIModule s_module;
	return s_module;
}

void FIModule::initialize(IApplication* app)
{
    m_app = app;

	registNetEvent();
	qbmsg::fixedincome::InitDecodeFuncs();
	qbmsg::exchangerepo::InitDecodeFuncs();
	qbmsg::irs::InitDecodeFuncs();
	reqData();
}

void FIModule::unInitialize()
{
	unRegistNetEvent();
	unRegistSpecificMsgProcessor();
}

int FIModule::execCommand(CmdInfo ci) const
{
	IMainFrame* frame = m_app->mainFrame();
	QString paraStr = QString::fromUtf8(ci.parameters);
	QString titleStr = QString::fromUtf8(ci.title);

    //模块可以自己决定是否多开
	int tabId = frame->existCmdWindow(ci.cmdId, paraStr);
	if (tabId != -1) {
 		frame->activateCmdWindow(tabId);
	}
	else {
		if (CMD_FixedIncomeMarketOverview == ci.cmdId){
			QWidget* widget = new FIOverviewsDlg(nullptr);
			frame->openCmdWindow(ci.cmdId, paraStr, widget, titleStr);
		}
		else {
			m_app->execCommand(ci.cmdId, ci.parameters);
		}
	}
	return 0;
}

bool FIModule::closeCommandWindow(int cmdId, QWidget* widget)
{
	if (!widget)
		return true;

	widget->setParent(nullptr);
	delete widget;

	return true;
}

void FIModule::registNetEvent()
{
	qbBase::getNetworkService()->RegisterNetworkEventObserver(this);
}

void FIModule::unRegistNetEvent()
{
	qbBase::getNetworkService()->DeRegisterNetworkEventObserver(this);
}

void FIModule::reqData() {
	TFBondDataCenter::instance().requestTFContactInfo();
}

STDMETHODIMP_(void) FIModule::OnRecover(THIS_ int32_t connect_id)
{
}
